//
//  YTAccountWithdrawViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/25.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTAccountWithdrawViewController.h"
#import "YTValidateIdentityViewController.h"
#import <CocoaSecurity/CocoaSecurity.h>
#import "YTWithdrawAccountCell.h"
#import "YTUserProfileServices.h"
#import "YTWithdrawAccountModel.h"

@interface YTAccountWithdrawViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (strong, nonatomic) NSMutableArray<YTWithdrawAccountModel *> *dataArray;
@end

@implementation YTAccountWithdrawViewController

- (void)initialize {
    _dataArray = [NSMutableArray array];
    @weakify(self);
    [[self rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id x) {
        @strongify(self);
        [self loadDataFromServer];
    }];
    
    [[_addBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        if ([YTUtilityManager manager].userModel.hasPayPassword != 1) {
            [UIAlertView bk_showAlertViewWithTitle:@"友情提示" message:@"您还未设置体现密码" cancelButtonTitle:@"忽略" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex != 1) return;
                [self performSegueWithIdentifier:@"YTSetEmbodyPassword" sender:nil];
            }];
        } else {
            [self performSegueWithIdentifier:@"YTAddWithdrawAccount" sender:nil];
        }
    }];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)loadDataFromServer {
    [[YTUserProfileServices loadWithdrawAccount] subscribeNext:^(NSDictionary *response) {
        NSDictionary *data = response[@"responseData"];
        _dataArray = [[NSArray yy_modelArrayWithClass:[YTWithdrawAccountModel class] json:data[@"accountList"]] mutableCopy];
        [self.tableView reloadData];
    }];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTWithdrawAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTWithdrawCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入提现密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
        @weakify(alert);
        [alert bk_setHandler:^{
            @strongify(alert);
            [self alertViewClick:indexPath payPassword:[alert textFieldAtIndex:0].text];
        } forButtonAtIndex:1];
        [alert show];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)alertViewClick:(NSIndexPath *)indexPath payPassword:(NSString *)payPassword {
    YTWithdrawAccountModel *model = _dataArray[indexPath.row];
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    dataDict[@"accountId"] = model.account_id;
    NSString *userID = [YTUtilityManager manager].userModel.userId;
    NSString *passwrod = [NSString stringWithFormat:@"%@%@paypassword", payPassword, userID];
    NSString *passwordMD5 = [CocoaSecurity md5:passwrod].hex;
    NSString *paySum = [NSString stringWithFormat:@"deleteAccount%@%@%@%@yitu8.net", model.name, model.number, userID, passwordMD5];
    dataDict[@"paySum"] = [CocoaSecurity md5:paySum].hex;
    [[YTUserProfileServices deleteAccount:dataDict] subscribeNext:^(NSDictionary *response) {
        [_dataArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [SVProgressHUD showSuccessWithStatus:@"删除成功"];
    } error:^(NSError *error) {
        [self setEditing:NO animated:YES];
        [SVProgressHUD showErrorWithStatus:@"删除失败,请稍后重试"];
    }];
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTSetEmbodyPassword"]) {
        YTValidateIdentityViewController *validate = segue.destinationViewController;
        validate.segueIdentifier = @"YTSetEmbody";
        validate.type = YTEmbodyPasswordSetType;
    }
}

@end
